package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.TDaAlarmDefinationBo;
import org.dromara.system.domain.vo.TDaAlarmDefinationVo;
import org.dromara.system.domain.TDaAlarmDefination;
import org.dromara.system.mapper.TDaAlarmDefinationMapper;
import org.dromara.system.service.ITDaAlarmDefinationService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 报警信息定义Service业务层处理
 *
 * @author Liu
 * @date 2023-11-23
 */
@RequiredArgsConstructor
@Service
public class TDaAlarmDefinationServiceImpl implements ITDaAlarmDefinationService {

    private final TDaAlarmDefinationMapper baseMapper;

    /**
     * 查询报警信息定义
     */
    @Override
    public TDaAlarmDefinationVo queryById(Long ID){
        return baseMapper.selectVoById(ID);
    }

    /**
     * 查询报警信息定义列表
     */
    @Override
    public TableDataInfo<TDaAlarmDefinationVo> queryPageList(TDaAlarmDefinationBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TDaAlarmDefination> lqw = buildQueryWrapper(bo);
        Page<TDaAlarmDefinationVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询报警信息定义列表
     */
    @Override
    public List<TDaAlarmDefinationVo> queryList(TDaAlarmDefinationBo bo) {
        LambdaQueryWrapper<TDaAlarmDefination> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TDaAlarmDefination> buildQueryWrapper(TDaAlarmDefinationBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TDaAlarmDefination> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getOpcServer()), TDaAlarmDefination::getOpcServer, bo.getOpcServer());
        lqw.like(StringUtils.isNotBlank(bo.getTagName()), TDaAlarmDefination::getTagName, bo.getTagName());
        lqw.eq(bo.getBit() != null, TDaAlarmDefination::getBit, bo.getBit());
        lqw.eq(StringUtils.isNotBlank(bo.getEquipCode()), TDaAlarmDefination::getEquipCode, bo.getEquipCode());
        lqw.eq(StringUtils.isNotBlank(bo.getAlarmCode()), TDaAlarmDefination::getAlarmCode, bo.getAlarmCode());
        lqw.eq(StringUtils.isNotBlank(bo.getName1()), TDaAlarmDefination::getName1, bo.getName1());
        lqw.eq(StringUtils.isNotBlank(bo.getName2()), TDaAlarmDefination::getName2, bo.getName2());
        lqw.eq(StringUtils.isNotBlank(bo.getName3()), TDaAlarmDefination::getName3, bo.getName3());
        lqw.eq(StringUtils.isNotBlank(bo.getName4()), TDaAlarmDefination::getName4, bo.getName4());
        lqw.eq(StringUtils.isNotBlank(bo.getName5()), TDaAlarmDefination::getName5, bo.getName5());
        lqw.eq(StringUtils.isNotBlank(bo.getName6()), TDaAlarmDefination::getName6, bo.getName6());
        lqw.eq(bo.getIsDelete() != null, TDaAlarmDefination::getIsDelete, bo.getIsDelete());
        lqw.eq(bo.getCreatedOn() != null, TDaAlarmDefination::getCreatedOn, bo.getCreatedOn());
        lqw.eq(bo.getPlantNO() != null, TDaAlarmDefination::getPlantNO, bo.getPlantNO());
        lqw.eq(StringUtils.isNotBlank(bo.getAlarmLevel()), TDaAlarmDefination::getAlarmLevel, bo.getAlarmLevel());
        return lqw;
    }

    /**
     * 新增报警信息定义
     */
    @Override
    public Boolean insertByBo(TDaAlarmDefinationBo bo) {
        TDaAlarmDefination add = MapstructUtils.convert(bo, TDaAlarmDefination.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setID(add.getID());
        }
        return flag;
    }

    /**
     * 修改报警信息定义
     */
    @Override
    public Boolean updateByBo(TDaAlarmDefinationBo bo) {
        TDaAlarmDefination update = MapstructUtils.convert(bo, TDaAlarmDefination.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TDaAlarmDefination entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除报警信息定义
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
